
import calendar_ex.*;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import javax.swing.text.BadLocationException;

/**
 *
 * @author Sean
 */
public class FilterPanel extends ObservingJPanel {
    
    private JPanel filterForm;
    private JLabel searchLabel;
    private JLabel tagsLabel;
    private JLabel hotspotLabel;
    private JComboBox tagsBox;
    private JCheckBox hotspotBox;
    private JButton okButton;
    private List<String> tags;
    protected CalendarEx calex;
    protected CalendarController control;
    
    public FilterPanel(CalendarEx cal, CalendarController controller) {
    super(cal, controller);
	this.calex = cal;
	this.control = controller;
        this.setLayout(new GridLayout(0, 2, 40, 60));

	searchLabel = new JLabel("Search");	
	tagsLabel = new JLabel("Tags");
	hotspotLabel = new JLabel("Availability Hotspot");
	

	tagsBox = new JComboBox();
	tagsBox.addItem("");

	tags = calex.getTags();
	for (int i=0; i < tags.size(); i++){
     	tagsBox.addItem(tags.get(i));
	}

	if(calex.getFilterTags().size() != 0){
		tagsBox.setSelectedItem(calex.getFilterTags().get(0));
	}
	

	hotspotBox = new JCheckBox("",calex.getHotSpot());	
	
	okButton = new JButton("Confirm");	

	this.add(new JLabel(""));
	this.add(new JLabel(""));
	this.add(tagsLabel);
	this.add(tagsBox);
	this.add(hotspotLabel);
	this.add(hotspotBox);
	this.add(new JLabel(""));
	this.add(okButton);
	this.add(new JLabel(""));
	this.add(new JLabel(""));
	this.add(new JLabel(""));	
	
	ActionListener okActionListener = new ActionListener() {
           	public void actionPerformed(ActionEvent e) {
        	control.changeView(IS3Calendar.View.MONTH);   	
      }
    };

        okButton.addActionListener(okActionListener);

	ActionListener boxActionListener = new ActionListener() {
           	public void actionPerformed(ActionEvent e) {
        	   	AbstractButton abstractButton = (AbstractButton) e.getSource();
        	   	boolean selected = abstractButton.getModel().isSelected();
        	   	calex.setHotSpot(selected);
      }
    };

	ActionListener tagActionListener = new ActionListener() {
      		public void actionPerformed(ActionEvent actionEvent) {
			calex.resetFilterTags();
			calex.addFilterTag(tagsBox.getSelectedItem().toString());
			if (tagsBox.getSelectedItem().toString() == ""){
				calex.resetFilterTags();
			}
      }
    };


       hotspotBox.addActionListener(boxActionListener);
       tagsBox.addActionListener(tagActionListener);
    }

    @Override
    public void update() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
